package com.zhongying.jf.util;

import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.math.BigDecimal;
import java.util.*;

/**
 * @description map取值util 多种取值方式返回对应的数据类型(已做非空判断)
 * 当map.get(key)不为某实例时,尝试将其转换为字符串再构建类型,成功返回对应类型,失败或者key不存在返回defaultValue
 * @auther zengli
 * @create 2017/9/27
 */
public class MapUtil {

    private Map<String, Object> map;


    public MapUtil(Map<String, Object> map) {
        this.map = map == null ? new HashMap<String, Object>() : map;
    }

    /**
     * @description getString方法做了特殊处理 只要key!=null就转成String返回
     * 该方法不考虑类型,如果需要对取值做后续数值运算请调用对应的取值类型方法
     * @auther zengli
     * @create 2017/9/27
     */
    public String getString(String key, String defaultValue) {
        if (map.get(key) == null) {
            return defaultValue;
        } else {
            return map.get(key).toString();
        }
    }

    public String getString(String key) {
        return getString(key,"");
    }

    public Integer getInteger(String key, Integer defaultValue) {
        if (map.get(key) != null) {
            if (map.get(key) instanceof Integer) {
                return (Integer) map.get(key);
            } else {
                try {
                    return new Integer(map.get(key).toString());
                } catch (NumberFormatException e) {
                }
            }
        }
        return defaultValue;
    }

    public Integer getInteger(String key) {
        return getInteger(key,null);
    }

    public Long getLong(String key, Long defaultValue) {
        if (map.get(key) != null) {
            if (map.get(key) instanceof Long) {
                return (Long) map.get(key);
            } else {
                try {
                    return new Long(map.get(key).toString());
                } catch (NumberFormatException e) {
                }
            }
        }
        return defaultValue;
    }

    public Long getLong(String key) {
        return getLong(key,null);
    }


    public Double getDouble(String key, Double defaultValue) {
        if (map.get(key) != null) {
            if (map.get(key) instanceof Double) {
                return (Double) map.get(key);
            } else {
                try {
                    return new Double(map.get(key).toString());
                } catch (Exception e) {
                }
            }
        }
        return defaultValue;
    }

    public Double getDouble(String key) {
        return getDouble(key,null);
    }

    public Float getFloat(String key, Float defaultValue) {
        if (map.get(key) != null) {
            if (map.get(key) instanceof Float) {
                return (Float) map.get(key);
            } else {
                try {
                    return new Float(map.get(key).toString());
                } catch (Exception e) {
                }
            }
        }
        return defaultValue;
    }

    public Float getFloat(String key) {
        return getFloat(key,null);
    }

    public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
        if (map.get(key) != null) {
            if (map.get(key) instanceof BigDecimal) {
                return (BigDecimal) map.get(key);
            } else {
                try {
                    return new BigDecimal(map.get(key).toString());
                } catch (Exception e) {
                }
            }
        }
        return defaultValue;
    }

    public BigDecimal getBigDecimal(String key) {
        return getBigDecimal(key,null);
    }

    public Boolean getBoolean(String key, Boolean defaultValue) {
        if (map.get(key) != null) {
            if (map.get(key) instanceof Boolean) {
                return (Boolean) map.get(key);
            } else {
                try {
                    return new Boolean(map.get(key).toString());
                } catch (Exception e) {
                }
            }
        }
        return defaultValue;
    }

    public Boolean getBoolean(String key) {
        return getBoolean(key,false);
    }

    public Date getDate(String key) {
        if (map.get(key) != null) {
            if (map.get(key) instanceof Date) {
                return (Date) map.get(key);
            }
        }
        return null;
    }

    public Map<Object, Object> getMap(String key) {
        if (map.get(key) != null) {
            if (map.get(key) instanceof Map) {
                return (Map<Object, Object>) map.get(key);
            }
        }
        return null;
    }

    /**
     * 根据key 获取指定类型对象
     * @param key
     * @param tClass
     * @param <T>
     * @auther zengli
     * @create 2017/10/21
     */
    public <T>T getObject(String key,Class<T> tClass) {
        if (map.get(key) != null) {
            try {
                return (T) map.get(key);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("int", "123");
        map.put("double", "11.113a");
        map.put("dec", new BigDecimal(123));
        map.put("date", new Date());
        MapUtil mapUtil = new MapUtil(map);
        System.out.println(mapUtil.getInteger("int", null));
        System.out.println(mapUtil.getDouble("double", 1D));
        System.out.println(mapUtil.getObject("date",Date.class).toString());
    }

} 